package com.example.petcare.service.serviceImpl;

import com.example.petcare.mapper.OrderPhotoMapper;
import com.example.petcare.pojo.OrderPhoto;
import com.example.petcare.pojo.PageBean;
import com.example.petcare.service.OrderPhotoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderPhotoServiceImpl implements OrderPhotoService {
    // 注入OrderPhotoMapper
    @Autowired
    private OrderPhotoMapper orderPhotoMapper;

    // 添加订单照片
    @Override
    public void addOrderPhoto(OrderPhoto orderPhoto) {

        // 设置时间
        orderPhoto.setUploadTime(LocalDateTime.now());

        // 设置记录日期
        orderPhoto.setRecordDate(LocalDate.now());


        // 调用mapper方法插入数据
        orderPhotoMapper.add(orderPhoto);
    }

    // 查看指定日期的记录
    @Override
    public PageBean<OrderPhoto> getPhotosByDate(Integer pageNum, Integer pageSize, Integer hostId, LocalDate localDate) {

        // 1. 创建 PageBean 对象
        PageBean<OrderPhoto> pb = new PageBean<>();

        // 2. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 3. 调用 mapper 方法查询数据
        List<OrderPhoto> list = orderPhotoMapper.getPhotosByDate(hostId, localDate);

        // 4. 强转成 Page 类型以便获取分页信息
        Page<OrderPhoto> p = (Page<OrderPhoto>) list;

        // 5. 封装到 PageBean 中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        return pb;
    }



}
